package show.me.the.money.config.reportSchema;

import java.util.HashMap;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Element;

public class ReportDefineFactory {
	private static Log logger = LogFactory.getLog(ReportDefineFactory.class);
	static HashMap<String,String> cls = new HashMap<String,String>();
	static{
		cls.put("default", "ctd.config.reportSchema.DefaultReportDefine");
		cls.put("groupByDic", "ctd.config.reportSchema.DictionaryReportDefine");
		cls.put("eval","ctd.config.reportSchema.EvalReportDefine");
		cls.put("composite","ctd.config.reportSchema.CompositeReportDefine");
	}
	
	public static ReportDefine createInstance(Element defineXML,int startIndex){
		String name = defineXML.attributeValue("type","default");
		String clsName = "";
		if(defineXML.attributeValue("class") != null){
			clsName = defineXML.attributeValue("class");
		}
		else{
			clsName = cls.get(name);
		}
		if(!StringUtils.isEmpty(clsName)){
			try {
				ReportDefine define = (ReportDefine)Class.forName(clsName).newInstance();
				define.setStartRenderIndex(startIndex);
				define.setDefineXML(defineXML);
				return define;
			}
			catch (Exception e) {
				e.printStackTrace();
				logger.error("ReportDefine for [" + name + "] create instance failed:", e);
			}

		}
		return null;
	}
}
